package com.cy.personer.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cy.personer.bean.Combo;
import com.cy.personer.bean.User;
import com.cy.personer.bean.UserCount;
import com.cy.personer.biz.impl.StatisBizImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/statis")
public class StatisHandler {

    @Resource
    private StatisBizImpl statisBizImpl;


    /**
     * 每周增加人数
     *
     * @author whl
     */

    @RequestMapping("statis.action")
    public String stat() {
        return "admin/statis";
    }

    /**
     * 用户周人数
     * @author whl
     * @param request
     * @return
     */
    @RequestMapping(value = ("statt.action"), method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public @ResponseBody
    int[] statis(HttpServletRequest request) {
        List<User> AllUser = statisBizImpl.AllUser();
        int[] data = {0, 0, 0, 0, 0, 0, 0};
        for (int i = 0; i < AllUser.size(); i++) {
            if (AllUser.get(i) != null) {
                data[i] = AllUser.get(i).getWeekcounts();
            }
        }
        return data;
    }

    /**
     * 月用户数
     *
     * @return
     */
    @RequestMapping(value = ("stati.action"), method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public @ResponseBody
    Map<String, Object> mouth(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        List<User> moth = statisBizImpl.month();
        int[] data = new int[moth.size()];
        String[] dataName = new String[moth.size()];
        for (int i = 0; i < moth.size(); i++) {
            if (moth.get(i).getWeekcounts() != null) {
                data[i] = moth.get(i).getWeekcounts();
            } else {
                data[i] = 0;
            }
            int j = i + 1;
            dataName[i] = "" + j + "号";
        }
        map.put("data", data);
        map.put("dataName", dataName);
        return map;
    }

    /**
     * 半年用户数
     *
     * @author whl
     */
    @RequestMapping(value = ("year.action"), method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public @ResponseBody
    Map<String, Object> year(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        List<User> yeart = statisBizImpl.year();
        int[] data = new int[yeart.size()];
        String[] dataName = new String[yeart.size()];
        for (int i = 0; i < yeart.size(); i++) {
            if (yeart.get(i).getHrefCounts() != null) {
                data[i] = yeart.get(i).getHrefCounts();
            } else {
                data[i] = 0;
            }
            int j = i + 1;
            dataName[i] = "" + yeart.get(i).getHrefMonths() + "月";
        }
        map.put("data", data);
        map.put("dataName", dataName);
        return map;
    }

    /**
     * 套餐使用数量统计
     *
     * @author whl
     */

    @RequestMapping("stat.action")
    public String week(HttpSession session) {
        List<Combo>lists=statisBizImpl.list();
        if (lists!=null){
            session.setAttribute("lists",lists);
        }else{

        }
        return "admin/stat";

    }

    //套餐周人数使用
    @RequestMapping(value = ("weekk.action"), method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public @ResponseBody
    int[] week(HttpServletRequest request,int comboId) {
        List<Combo> weck = statisBizImpl.week(comboId);
        int[] data = {0, 0, 0, 0, 0, 0, 0};
        for (int i = 0; i < weck.size(); i++) {
            if (weck.get(i) != null) {
                data[i] = weck.get(i).getWeekcounts();
            }
        }
        return data;
    }

    //套餐月人数使用
    @RequestMapping(value = ("mouth.action"), method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public @ResponseBody
    Map<String, Object> mou(HttpServletRequest request,int comboId) {
        Map<String, Object> map = new HashMap<>();
        List<Combo> moth = statisBizImpl.mont(comboId);
        int[] data = new int[moth.size()];
        String[] dataName = new String[moth.size()];
        for (int i = 0; i < moth.size(); i++) {
            if (moth.get(i).getWeekcounts() != null) {
                data[i] = moth.get(i).getWeekcounts();
            } else {
                data[i] = 0;
            }
            int j = i + 1;
            dataName[i] = "" + j + "号";
        }
        map.put("data", data);
        map.put("dataName", dataName);
        return map;
    }

    //套餐近半年人数使用
    @RequestMapping(value = ("yearth.action"), method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public @ResponseBody
    Map<String, Object> yeart(HttpServletRequest request,int comboId) {
        Map<String, Object> map = new HashMap<>();
        List<Combo> yeart = statisBizImpl.yeart(comboId);
        int[] data = new int[yeart.size()];
        String[] dataName = new String[yeart.size()];
        for (int i = 0; i < yeart.size(); i++) {
            if (yeart.get(i).getHrefCounts() != null) {
                data[i] = yeart.get(i).getHrefCounts();
            } else {
                data[i] = 0;
            }
            int j = i + 1;
            dataName[i] = "" + yeart.get(i).getHrefMonths() + "月";
        }
        map.put("data", data);
        map.put("dataName", dataName);
        return map;
    }

}